Passed
Push — master ( 56b7f2...fe4dd1 )
by Rafael S.
01:22
created

index.js ➔ packValue   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 5
rs 9.4285
1
/*!
2
 * byte-data
3
 * Readable data to and from byte buffers.
4
 * Copyright (c) 2017 Rafael da Silva Rocha.
5
 * https://github.com/rochars/byte-data
6
 *
7
 */
8
9
const rw = require("./src/read-write");
10
const helpers = require("./src/helpers");
11
let Type = require("./src/type");
12
13
/**
14
 * Turn a number or string into a byte buffer.
15
 * @param {number|string} value The value.
16
 * @param {Object} type One of the available types.
17
 * @param {number} base The base of the output. Optional. Default is 10.
18
 * @return {!Array<number>|!Array<string>}
19
 */
20
function packValue(value, type, base=10) {
21
    let theType = helpers.getType(type, base, true);
22
    value = theType.char ? value.slice(0, type.bits / 8) : value;
23
    return rw.toBytes(helpers.turnToArray(value), theType);
24
}
25
26
/**
27
 * Turn a byte buffer into a readable value.
28
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes.
29
 * @param {Object} type One of the available types.
30
 * @param {number} base The base of the input. Optional. Default is 10.
31
 * @return {number|string}
32
 */
33
function unpackValue(buffer, type, base=10) {
34
    return rw.fromBytes(buffer, helpers.getType(type, base, true));
35
}
36
37
/**
38
 * Turn a array of numbers into a byte buffer.
39
 * @param {!Array<number>|string} values The values.
40
 * @param {Object} type One of the available types.
41
 * @param {number} base The base of the output. Optional. Default is 10.
42
 * @return {!Array<number>|!Array<string>}
43
 */
44
function packArray(values, type, base=10) {
45
    return rw.toBytes(values, helpers.getType(type, base, false));
46
}
47
48
/**
49
 * Turn a byte array into a sequence of readable values.
50
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array.
51
 * @param {Object} type One of the available types.
52
 * @param {number} base The base of the input. Optional. Default is 10.
53
 * @return {!Array<number>|string}
54
 */
55
function unpackArray(buffer, type, base=10) {
56
    return rw.fromBytes(buffer, helpers.getType(type, base, false));
57
}
58
59
/**
60
 * Find and return the start index of some string.
61
 * Return -1 if the string is not found.
62
 * @param {!Array<number>|Uint8Array} buffer A byte buffer.
63
 * @param {string} text Some string to look for.
64
 * @return {number} The start index of the first occurrence, -1 if not found
65
 */
66
function findString(buffer, text) {
67
    let found = "";
68
    for (let i = 0; i < buffer.length; i++) {
69
        found = unpackValue(
70
                buffer.slice(i, i + text.length + 1),
71
                new Type({"bits": text.length * 8, "char": true})
72
            );
73
        if (found == text) {
74
            return i;
75
        }
76
    }
77
    return -1;
78
}
79
80
/**
81
 * Turn a struct into a byte buffer.
82
 * A struct is an array of values of not necessarily the same type.
83
 * @param {Array} struct The struct values.
84
 * @param {!Array<Object>} def The struct type definition.
85
 * @param {number} base The base of the output. Optional. Default is 10.
86
 * @return {!Array<number>|!Array<string>}
87
 */
88
function packStruct(struct, def, base=10) {
89
    if (struct.length < def.length) {
90
        return [];
91
    }
92
    let bytes = [];
93
    for (let i = 0; i < def.length; i++) {
94
        bytes = bytes.concat(packValue(struct[i], def[i], base));
95
    }
96
    return bytes;
97
}
98
99
/**
100
 * Turn a byte buffer into a structure.
101
 * A struct is an array of values of not necessarily the same type.
102
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte buffer.
103
 * @param {!Array<Object>} def The struct type definition.
104
 * @param {number} base The base of the input. Optional. Default is 10.
105
 * @return {Array}
106
 */
107
function unpackStruct(buffer, def, base=10) {
108
    if (buffer.length < getStructBits(def)) {
109
        return [];
110
    }
111
    let struct = [];
112
    let i = 0;
113
    let j = 0;
114
    while (i < def.length) {
115
        let bits = def[i].bits < 8 ? 1 : def[i].bits / 8;
116
        struct = struct.concat(
117
                unpackValue(buffer.slice(j, j + bits), def[i], base)
118
            );
119
        j += bits;
120
        i++;
121
    }
122
    return struct;
123
}
124
125
function getStructBits(def) {
126
    let bits = 0;
127
    for (let i = 0; i < def.length; i++) {
128
        bits += def[i].bits / 8;
129
    }
130
    return bits;
131
}
132
133
// interface
134
module.exports.pack = packValue;
135
module.exports.unpack = unpackValue;
136
module.exports.packArray = packArray;
137
module.exports.unpackArray = unpackArray;
138
module.exports.unpackStruct = unpackStruct;
139
module.exports.packStruct = packStruct;
140
module.exports.findString = findString;
141
module.exports.Type = Type;
142
143
// types
144
module.exports.chr = new Type({"bits": 8, "char": true});
145
module.exports.fourCC = new Type({"bits": 32, "char": true});
146
module.exports.bool = new Type({"bits": 1});
147
module.exports.int2 = new Type({"bits": 2, "signed": true});
148
module.exports.uInt2 = new Type({"bits": 2});
149
module.exports.int4 = new Type({"bits": 4, "signed": true});
150
module.exports.uInt4 = new Type({"bits": 4});
151
module.exports.int8 = new Type({"bits": 8, "signed": true});
152
module.exports.uInt8 = new Type({"bits": 8});
153
// LE
154
module.exports.int16  = new Type({"bits": 16, "signed": true});
155
module.exports.uInt16 = new Type({"bits": 16});
156
module.exports.float16 = new Type({"bits": 16, "float": true});
157
module.exports.int24 = new Type({"bits": 24, "signed": true});
158
module.exports.uInt24 = new Type({"bits": 24});
159
module.exports.int32 = new Type({"bits": 32, "signed": true});
160
module.exports.uInt32 = new Type({"bits": 32});
161
module.exports.float32 = new Type({"bits": 32, "float": true});
162
module.exports.int40 = new Type({"bits": 40, "signed": true});
163
module.exports.uInt40 = new Type({"bits": 40});
164
module.exports.int48 = new Type({"bits": 48, "signed": true});
165
module.exports.uInt48 = new Type({"bits": 48});
166
module.exports.float64 = new Type({"bits": 64, "float": true});
167
// BE
168
module.exports.int16BE  = new Type({"bits": 16, "signed": true, "be": true});
169
module.exports.uInt16BE = new Type({"bits": 16, "be": true});
170
module.exports.float16BE = new Type({"bits": 16, "float": true, "be": true});
171
module.exports.int24BE = new Type({"bits": 24, "signed": true, "be": true});
172
module.exports.uInt24BE = new Type({"bits": 24, "be": true});
173
module.exports.int32BE = new Type({"bits": 32, "signed": true, "be": true});
174
module.exports.uInt32BE = new Type({"bits": 32, "be": true});
175
module.exports.float32BE = new Type({"bits": 32, "float": true, "be": true});
176
module.exports.int40BE = new Type({"bits": 40, "signed": true, "be": true});
177
module.exports.uInt40BE = new Type({"bits": 40, "be": true});
178
module.exports.int48BE = new Type({"bits": 48, "signed": true, "be": true});
179
module.exports.uInt48BE = new Type({"bits": 48, "be": true});
180
module.exports.float64BE = new Type({"bits": 64, "float": true, "be": true});
181